import React, {FC, useCallback, useMemo} from "react";
import {message} from "antd";
import {useTranslation} from "react-i18next";
import CreateModal from "../CreateModal.tsx";
import {JsEncryptPsw} from "../../../services/SecurityService.tsx";

interface ChangePasswordModalProps {
    modalKey: string
}

const ChangePasswordModal: FC<ChangePasswordModalProps> = (props) => {
    const {t} = useTranslation();

    const beforeSubmit = useCallback(async (params: any, view: any) => {
        if (params.newPass && params.newPass !== params.verifyPass) {
            message.warning(t("passwordCheck"));
            return false;
        }
        return {
            oldPassword: await JsEncryptPsw(params.pass),
            newPassword: await JsEncryptPsw(params.newPass),
            confirmPassword: await JsEncryptPsw(params.verifyPass)
        }
    }, []);

    const renderChangePasswordModal = useMemo(() => {
        return (
            <CreateModal
                {...props}
                beforeSubmit={beforeSubmit}
            />
        )
    }, []);

    return (renderChangePasswordModal);
};

export default ChangePasswordModal;